package academic.ui.client.presenter;


import academic.ui.client.AppServiceAsync;
import academic.ui.client.CustomerServiceAsync;
import academic.ui.client.jquery.Facebook;
import academic.ui.client.jquery.JQuery;
import academic.ui.client.place.Tokens;
import academic.ui.client.widgets.DatePicker;
import academic.ui.client.widgets.forms.BuyAsGiftForm;
import academic.ui.client.widgets.forms.CreateEventForm;
import academic.ui.client.widgets.tools.Overlay;


import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.EventBus;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyStandard;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.PlaceRequest;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.gwtplatform.mvp.client.proxy.RevealContentEvent;

import ecommerce.soco.shared.GWT_Event;
import ecommerce.soco.shared.GWT_Product;


public class CreateEventPresenter extends Presenter<CreateEventPresenter.MyView,CreateEventPresenter.MyProxy>
{

	private final AppServiceAsync service;
	
	private final PlaceManager placeManager;
	
	@Inject 
	public CreateEventPresenter(EventBus eventBus, MyView view, MyProxy proxy,AppServiceAsync service,PlaceManager placeManager) {
		super(eventBus, view, proxy);
		this.service = service;
		this.placeManager=placeManager;
	}

	@ProxyStandard	
	@NameToken(Tokens.createEvent)
	public interface MyProxy extends ProxyPlace<CreateEventPresenter>{

	}

	
	public interface MyView extends View {

		void setup(GWT_Product result);

		Boolean checkRules();

		HasClickHandlers cancel();

		HasClickHandlers submit();

		CreateEventForm getForm();
	}
	
	@Override
	public void onReveal() {
		super.onReveal();
		Log.debug("CreateEventPresenter onReveal");
		service.getCurrentProduct(new AsyncCallback<GWT_Product>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(GWT_Product result) {
				getView().setup(result);
			}
			
		});
	}
	
	private String selector;
	
	@Override
	public void onBind() {
		super.onBind();
		Log.debug("CreateEventPresenter onBind");	
		
		registerHandler(getView().submit().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				if(!getView().checkRules())
					JQuery.fadeInErrors();
				else
				{
					CreateEventForm form = getView().getForm();
					Log.debug(form.getEventName()+" "+form.getDescription()+" "+form.getDatePicker().getStartDateTime()+" "+form.getDatePicker().getEndDateTime());
					service.createEvent(form.getEvent(), new AsyncCallback<GWT_Event>() {

						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub
							
						}

						@Override
						public void onSuccess(GWT_Event event) {
							Facebook.createEvent(event.getName(), event.getDescription(), String.valueOf(event.getStartdate()), String.valueOf(event.getEnddate()), event.getLocation(), event.getVenue());
						}
						
					});
					//service.saveEvent(form.getEventName(),form.getDescription(),form.getStartTime(),form.getEndTime());
					onHide();
				}
			}
			
		}));
	}
    
	@Override
	protected void revealInParent() {
		RevealContentEvent.fire(this, AppPresenter.slot, this);
		Log.debug("CreateEventPresenter revealInParent");
	}
	
	@Override
	public void onReset() {
		super.onReset();
		
		
		DeferredCommand.addCommand(new Command() {

			@Override
			public void execute() {
				Log.debug("CreateEventPresenter onReset DeferredCommand");
				Overlay.initLink("#"+selector);
				DatePicker.init();
			}
			
		});		
		Log.debug("CreateEventPresenter onReset");
	}
	
	@Override
	public void prepareFromRequest(PlaceRequest request) {
		selector = request.getParameter("selector", null);
	}
	
	@Override
	public void onHide() {
		super.onHide();
		Log.debug("CreateEventPresenter onHide");
		Overlay.hideLink("#"+selector);
	}

}
